package com.nongcai159.mall.lib_common.utils

import android.content.Context
import android.os.Handler
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import com.nongcai159.mall.lib_common.base.BaseApplication

/**
 * 软键盘工具类
 *
 *
 * Created by tanbiheng on 2017/9/19.
 */

object KeyBoardUtils {

    /**
     * 打开软键盘
     *
     * @param mEditText 输入框
     */
    fun showKeybord(mEditText: EditText?) {
        if (mEditText != null) {
            val imm = BaseApplication.INSTANCE!!
                    .getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN)
            imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
                    InputMethodManager.HIDE_IMPLICIT_ONLY)
        }
    }

    /**
     * 关闭软键盘
     *
     * @param mEditText 输入框
     */
    fun hideKeybord(mEditText: EditText?) {
        if (mEditText != null) {
            val imm = BaseApplication.INSTANCE!!
                    .getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager

            imm.hideSoftInputFromWindow(mEditText.windowToken, 0)
        }
    }

    /**
     * @param @param editText
     * @param @param action<br></br>
     * EditorInfo.IME_ACTION_SEARCH 搜索 <br></br>
     * EditorInfo.IME_ACTION_SEND 发送<br></br>
     * EditorInfo.IME_ACTION_NEXT 下一步<br></br>
     * EditorInfo.IME_ACTION_DONE 完成<br></br>
     * EditorInfo.IME_ACTION_NEXT 去往<br></br>
     * @return void
     * @Desp: 设置输入法中回车按钮的显示内容
     */
    fun setImeOptions(editText: EditText, action: Int) {
        editText.imeOptions = action
    }

    /**
     * 输入框获取焦点
     *
     * @param mEditText
     */
    fun requestFocus(mEditText: EditText?) {
        if (mEditText != null) {
            Handler().postDelayed({
                mEditText.requestFocus()
                mEditText.isFocusable = true
                mEditText.isFocusableInTouchMode = true
            }, 50)
        }
    }

    /**
     * 输入框获取焦点弹出输入框
     *
     * @param mEditText
     */
    fun requestShowKeyBord(mEditText: EditText?) {
        if (mEditText != null) {
            Handler().postDelayed({
                mEditText.requestFocus()
                mEditText.isFocusable = true
                mEditText.isFocusableInTouchMode = true

                showKeybord(mEditText)
            }, 50)
        }
    }
}
